package cn.hpclub.server.util;

import com.jfinal.render.Render;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Describe: ͨ���ļ����ķ�ʽ����excel�ļ�
 * Created by zenghl on 2016/7/6.
 */
public class ExcelRenderUtil extends Render{

    private SXSSFWorkbook workbook;
    private String fileName;
    public ExcelRenderUtil(){

    }
    public ExcelRenderUtil(SXSSFWorkbook workbook,String fileName){
        this.fileName = fileName;
        this.workbook = workbook;
    }
    public ExcelRenderUtil(SXSSFWorkbook workbook){
        this.workbook = workbook;
    }
    @Override
    public void render() {
        try {
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HHmmssSSS");
            String curTime = sdf.format(date);


            fileName = fileName+curTime;
            response.setHeader("Content-Disposition", "attachment;fileName="+ java.net.URLEncoder.encode(fileName, "UTF-8")+".xlsx");
            OutputStream out = response.getOutputStream();
            workbook.write(out);
            out.flush();
            workbook.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
